package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.bumptech.glide.Glide;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.pojo.ArticleMessage;

import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;


public class CelebrityActivity extends BaseActivity {

//    @BindView(R.id.title)
//    TextView title;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.profile_image)
    ImageView profileImage;
    @BindView(R.id.name)
    TextView name;

    private ArticleMessage am;
    private LevelListDrawable mDrawable = new LevelListDrawable();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_celebrity;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);
        ButterKnife.bind(this);
        am = (ArticleMessage) this.getIntent().getSerializableExtra("am");
        Glide.with(mActivity).load(am.getCover()).into(profileImage);

//        title.setText(am.getTitle());
        content.setText(Html.fromHtml(am.getContent(), Html.FROM_HTML_MODE_COMPACT, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(final String source) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mDrawable.addLevel(0, 0, getResources().getDrawable(R.mipmap.ic_launcher));
                        mDrawable.setBounds(0, 0, 200, 200);

                        Bitmap bitmap;
                        try {
                            bitmap = BitmapFactory.decodeStream(new URL(source).openStream());
                            Message msg = handler.obtainMessage();
                            msg.what = 1123;
                            msg.obj = bitmap;
                            handler.sendMessage(msg);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                return mDrawable;
            }
        }, null));

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {


                case 1123:
                    Bitmap bitmap = (Bitmap) msg.obj;
                    BitmapDrawable drawable = new BitmapDrawable(null, bitmap);
                    mDrawable.addLevel(1, 1, drawable);
                    mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    mDrawable.setLevel(1);

                    CharSequence charSequence = content.getText();
                    content.setText(charSequence);
                    content.invalidate();
                    break;

            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}